package com.cindata

import grails.transaction.Transactional
import net.sf.json.JSON
import org.springframework.security.access.annotation.Secured

import static org.springframework.http.HttpStatus.*

@Transactional
@Secured(['isAuthenticated()'])
class RiskWarningConfigController {

    def componentService
    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def index() {
        def result = RiskWarningConfigure.list().get(0)
        respond result, model: [result: result, componentService: componentService],view: 'riskWarningConfig'
    }

    def modifyValue(){
        def id = params['id']
        def minValue = params['minValue']
        def isAuto = params['isAuto']
        def duration = params['duration']
        def workflowId = params['workflow']
        def riskWarningConfig = RiskWarningConfig.findById(id)

        if (!riskWarningConfig)
        {
            render(["failed"] as JSON)
            return
        }
        def workflow = null
        if(workflowId){
            workflow = Workflow.findById(Long.parseLong(workflowId))
        }
        def code = riskWarningConfig?.code
        if(code == '1' || code == '2'|| code == '8'|| code == '9'){
            riskWarningConfig.minValue = new BigDecimal(minValue).divide(new BigDecimal("100"))
        }else{
            riskWarningConfig.minValue = new BigDecimal(minValue)
        }
        riskWarningConfig.isAuto = Boolean.parseBoolean(isAuto)
        riskWarningConfig.duration = duration
        riskWarningConfig.workflow = workflow
        riskWarningConfig.save(flush: true)
        redirect view: 'index'
    }

    def changeValue(){
        def id = params['id']
        def value = params['value']
        def isAuto = params['isAuto']
        def duration = params['duration']
        def workflowId = params['workflow']
        def riskWarningConfig = RiskWarningConfig.findById(id)

        if (!riskWarningConfig)
        {
            render(["failed"] as JSON)
            return
        }
        def workflow = null
        if(workflowId){
            workflow = Workflow.findById(Long.parseLong(workflowId))
        }

        riskWarningConfig.value = value
        riskWarningConfig.isAuto = Boolean.parseBoolean(isAuto)
        riskWarningConfig.duration = duration
        riskWarningConfig.workflow = workflow
        riskWarningConfig.save(flush: true)
        redirect view: 'index'
    }

    def show(RiskWarningConfig riskWarningConfig) {
        respond riskWarningConfig
    }

    def create() {
        respond new RiskWarningConfig(params)
    }

    @Transactional
    def save(RiskWarningConfig riskWarningConfig) {
        if (riskWarningConfig == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (riskWarningConfig.hasErrors()) {
            transactionStatus.setRollbackOnly()
            respond riskWarningConfig.errors, view:'create'
            return
        }

        riskWarningConfig.save flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.created.message', args: [message(code: 'riskWarningConfig.label', default: 'RiskWarningConfig'), riskWarningConfig.id])
                redirect riskWarningConfig
            }
            '*' { respond riskWarningConfig, [status: CREATED] }
        }
    }

    def edit(RiskWarningConfig riskWarningConfig) {
        respond riskWarningConfig
    }

    @Transactional
    def update(RiskWarningConfig riskWarningConfig) {
        if (riskWarningConfig == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (riskWarningConfig.hasErrors()) {
            transactionStatus.setRollbackOnly()
            respond riskWarningConfig.errors, view:'edit'
            return
        }

        riskWarningConfig.save flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'riskWarningConfig.label', default: 'RiskWarningConfig'), riskWarningConfig.id])
                redirect riskWarningConfig
            }
            '*'{ respond riskWarningConfig, [status: OK] }
        }
    }

    @Transactional
    def delete(RiskWarningConfig riskWarningConfig) {

        if (riskWarningConfig == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        riskWarningConfig.delete flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'riskWarningConfig.label', default: 'RiskWarningConfig'), riskWarningConfig.id])
                redirect action:"index", method:"GET"
            }
            '*'{ render status: NO_CONTENT }
        }
    }

    protected void notFound() {
        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'riskWarningConfig.label', default: 'RiskWarningConfig'), params.id])
                redirect action: "index", method: "GET"
            }
            '*'{ render status: NOT_FOUND }
        }
    }
}
